package com.halachawiki.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.HasDirection.Direction;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.halachawiki.client.datepicker.DatePicker;
import com.halachawiki.client.richtexteditor.RichTextToolbar;
import com.halachawiki.shared.HalachicPeriods;
import com.halachawiki.shared.PublicAuthor;

public class AuthorEditor extends VerticalPanel {
	private AuthorConstants Constants = GWT.create(AuthorConstants.class);
	//private AuthorMessages Messages = GWT.create(Authors.class);

	TextBox tbName = new TextBox();
	MultipleStringInput msNickNames = new MultipleStringInput(null);
	RichTextArea about = new RichTextArea();

	DatePicker birthDate = new DatePicker();
	DatePicker deathDate = new DatePicker();
	Button submitButton = new Button(Constants.saveButton());
	Button cancelButton = new Button(Constants.cancelButton());
	
	public AuthorEditor(final PublicAuthor author, final AsyncCallback<PublicAuthor> callback) {
		HorizontalPanel hp = new HorizontalPanel();
		add(hp);
		hp.add(new Label(Constants.nameColumnHeader()));
		hp.add(tbName);
		
		HorizontalPanel hpNickNames = new HorizontalPanel();
		add(hpNickNames);
		hp.add(new Label(Constants.nicknameColumnHeader()));
		hp.add(msNickNames);
		
		final ListBox period = new ListBox();
		add(period);
		period.addItem("");
		for (HalachicPeriods a_period : HalachicPeriods.allPeriods)
			period.addItem(a_period.getDisplayString(), Direction.RTL);
		
		add(new Label(Constants.aboutField()));
		about.setSize("100%", "14em");
		about.addStyleName("bounding-box");
		RichTextToolbar toolbar = new RichTextToolbar(about);
		toolbar.setWidth("100%");

		// Add the components to a panel
		Grid grid = new Grid(2, 1);
		grid.setStyleName("cw-RichText");
		grid.setWidget(0, 0, toolbar);
		grid.setWidget(1, 0, about);

		add(grid);

		HorizontalPanel datePanel = new HorizontalPanel();
		add(datePanel);
		datePanel.addStyleName("date-panel");
		datePanel.setSpacing(5);
		
		VerticalPanel vp1 = new VerticalPanel();
		datePanel.add(vp1);
		vp1.addStyleName("bounding-box");
		vp1.add(new Label(Constants.birthDateField()));
		vp1.add(birthDate);

		VerticalPanel vp2 = new VerticalPanel();
		datePanel.add(vp2);
		vp2.addStyleName("bounding-box");
		vp2.add(new Label(Constants.deathDateField()));
		vp2.add(deathDate);
	
		HorizontalPanel buttons = new HorizontalPanel();
		buttons.add(cancelButton);
		buttons.add(submitButton);
		add(buttons);
		
		// initialize data
		if (author != null) {
			tbName.setText(author.name);
			msNickNames.setValue(author.nickNames);
			about.setHTML(author.description); // TODO: Switch to SafeHtml
			birthDate.setDate(author.birthDate);
			deathDate.setDate(author.deathDate);
			if (author.period != null)
				period.setSelectedIndex(author.period.getIndex());
		}
		
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				callback.onSuccess(null);
			} 
		});
		submitButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				PublicAuthor returnAuthor = new PublicAuthor();
				if (author != null) {
					returnAuthor.versionID = author.versionID;
				}
				returnAuthor.name = tbName.getText();
				returnAuthor.nickNames = msNickNames.getValue();
				returnAuthor.description = about.getHTML();
				returnAuthor.birthDate = birthDate.getDate();
				returnAuthor.deathDate = deathDate.getDate();
				returnAuthor.period = HalachicPeriods.getPeriod(period.getSelectedIndex());
				
				callback.onSuccess(returnAuthor);
			} 
		});
		
	}
}
